package com.qf.dao.impl;

import com.qf.dao.UserDao;

import com.qf.pojo.User;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    QueryRunner qr =new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public User loginByNameAndPwd(String userName, String userPwd) {
        String sql=" select * from user where user_name=? and user_pwd=? ";
        try {
            return qr.query(sql,new BeanHandler<>(User.class),userName,userPwd);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询用户名或密码不正确");
        }
        return null;
    }

    @Override
    public void insert(String username, String password) {

        String sql=" insert into user (user_name,user_pwd)values(?,?)";
        try {
            qr.update(sql,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User selectUserName(String userName) {

        String sql="select * from user where user_name=?";
        try {
            return qr.query(sql,new BeanHandler<>(User.class),userName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
